package com.wuyou.mid;


import java.util.Arrays;

/**
 * @Description 80. 删除有序数组中的重复项 II
 * @Author yh.zhang
 * @Date 2023/11/23 11:00
 */
public class RemoveDuplicates {


    public static void main(String[] args) {
//        int[] nums = {0,0,1,1,1,1,2,3,3};
        int[] nums = {1};

        System.out.println(solution_1(nums));
    }

    public static int solution_2(int[] nums){
        int length = nums.length;
        if (length <= 2) {
            return length;
        }
        int slow = 0, fast = 0;
        while (fast < length) {
            if(nums[slow-2] != nums[fast]){
                nums[slow++] = nums[fast];
            }
            fast++;
        }
        return slow;
    }

    public static int solution_1(int[] nums){
        int ans = nums.length;
        int curCount = 1, preNum = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if(preNum == nums[i]){
                curCount ++;
                if (curCount > 2) {
                    nums[i] = Integer.MAX_VALUE;
                    ans--;
                }
            }else {
                curCount = 1;
                preNum = nums[i];
            }
        }
        Arrays.sort(nums);
        return ans;
    }
}
